package com.jt.huangxiaolong.demos.service.impl;

import com.jt.huangxiaolong.demos.mapper.UserMapper;
import com.jt.huangxiaolong.demos.pojo.User;
import com.jt.huangxiaolong.demos.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Override
    public List<User> allUserPage() {
        List<User> users = userMapper.allUserPage();
        return users;
    }

    @Override
    public int saveDateCheck(User user) {
        if (!checkName(user.getDName())){
            return -1;
        }
        return userMapper.insert(user);
    }

    @Override
    public List<User> selectByDName(String dName,String dType,String dState) {
        return userMapper.selectByDName(dName,dType,dState);
    }

    @Override
    public List<User> allpage() {
        List<User> users = userMapper.allpage();
        return users;
    }

    @Override
    public int update(User user) {

        String isDelete = userMapper.selectIsDeleteById(user.getId());
        System.out.println(isDelete);
        if (isDelete.equals("正常")){
            return userMapper.update(user);
        }else {
            return 0;
        }
    }

    @Override
    public void deleteAll(List<Integer> ids) {
        userMapper.deleteAll(ids);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateAll(User user) {

        String isDelete = userMapper.selectIsDeleteById(user.getId());
        System.out.println(isDelete);
        if (isDelete.equals("正常")){
            return userMapper.updateAll(user);
        }else {
            return 0;
        }
    }


    private boolean checkName(String dName){

        User user=new User();
        user.setDName(dName);
        List<User> collegeList=userMapper.select1(dName);
        if(collegeList.size()>0){
            return false;
        }
        return true;
    }
}
